Programming Questions for Practice
Find Second largest element in an array
#include <stdio.h>
#include <stdlib.h>
int main()
{
int size;
printf("\nEnter the size of the array: ");
scanf("%d", &size);
int array[size];
printf("\nEnter any %d numbers: ", size);
for(int i = 0; i < size; i++)
scanf("%d", &array[i]);
// Logic
if(size < 2) {
printf("\nThere is no second element\n");
return;
}
// Sorting the array
int temp;
for(int i = 0; i < size; i++){
for(int j = i+1; j < size; j++){
if(array[i] < array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
printf("\nSecond largest element: %d\n", array[1]);
return 0;
}